package cn.theten52.demo.patterns.design.behavior.interpreter.update1;

import static cn.theten52.demo.patterns.design.behavior.interpreter.update1.Calculator.MINUS;
import static cn.theten52.demo.patterns.design.behavior.interpreter.update1.Calculator.PLUS;

/**
 * 表示所有操作数
 * terminal expression
 *
 * @author wangjin
 * @date 2021/1/6
 */
public class Operand implements Expression {

    private int number;

    public Operand(char c) {
        this.number = (int)c - 48;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    /**
     * 计算表达式
     *
     * @param context 上下文
     */
    @Override
    public void evaluate(Context context) {
        switch (context.getOperator()) {
            case PLUS:
                context.setValue(context.getValue() + number);
                break;
            case MINUS:
                context.setValue(context.getValue() - number);
                break;
            default:
                context.setValue(number);
        }
    }
}
